using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using DG.Tweening.Core;
using QFramework;
using Sirenix.OdinInspector;
using UnityEngine;

public class AudioSystem :AbstractSystem
{
    //全局音量(float,0~1),音量设定为50%
    private float m_GlobalVolume = 0.5f;
    //背景音乐音量(float,0~1)，音量设定为50%
    private float m_BGVolume = 0.5f;
    //特效音乐音量(flaot,0~1)
    private float m_EffectVolume = 0.5f;
    private GameObject m_AudioSourceGO;
    private AudioSource m_BGMAudioSource;
    private AudioSource m_EffectAudioSource;
    private string m_CurrentBGM = null;
    private string m_CurrentEffect;
    List<AudioSource> audioList = new List<AudioSource>();

    protected override void OnInit()
    {
        m_AudioSourceGO = new GameObject("AudioSource");
        GameObject.DontDestroyOnLoad(m_AudioSourceGO);
        m_BGMAudioSource = m_AudioSourceGO.AddComponent<AudioSource>();
        m_BGMAudioSource.loop = true;
        m_EffectAudioSource = m_AudioSourceGO.AddComponent<AudioSource>();
        SetBGM("Sound/bgm/puka_puka");
        this.RegisterEvent<SoundInfo>(PlaySound);
    }

    protected override void OnDeinit()
    {
        GameObject.Destroy(m_AudioSourceGO);
    }

    void PlaySound(SoundInfo info)
    {
        var path = info.path;
        PlaySoundEffect(path);
    }
    

    public async UniTaskVoid SetBGM(string bgmName)
    {
        if (string.IsNullOrEmpty(m_CurrentBGM))   // 第一次设置背景音乐、直接设置
        {
            Debug.Log(bgmName);
            m_BGMAudioSource.clip = (AudioClip)await Resources.LoadAsync(bgmName);
            m_CurrentBGM = bgmName;
            m_BGMAudioSource.Play();
        }
        else if (m_CurrentBGM != bgmName)
        {
            var clip = (AudioClip)await Resources.LoadAsync(bgmName);
            
            TransitionBGM(clip,bgmName).Forget();
        }
    }

    public AudioSource getEmptyAudioSource()
    {
        foreach (var audioScoure in audioList)
        {
            if (!audioScoure.isPlaying) return audioScoure;
        }
        AudioSource audioSource = m_AudioSourceGO.AddComponent<AudioSource>();
        audioList.Add(audioSource);
        return audioSource;
    }

    public async UniTaskVoid PlaySoundEffect(string soundName)
    {
        var clip = (AudioClip)await Resources.LoadAsync(soundName);
        var audioSource = getEmptyAudioSource();
        audioSource.clip = clip;
        audioSource.Play();
    }

    public void StopAllSound()
    {
        foreach (var audioSource in audioList)
        {
            audioSource.Pause();
        }
    }
    
    // public void 

    public void PlayClickSound()
    {
        PlaySoundEffect("audio_click").Forget();
    }

    private async UniTaskVoid TransitionBGM(AudioClip nextBGM, string nextBGMName)
    {
        var vol2ZeroTweener = DOTween.To(() => m_BGMAudioSource.volume, delegate (float x)
        {
            m_BGMAudioSource.volume = x;
        }, 0f, 1.5f);
        await vol2ZeroTweener;
        m_BGMAudioSource.clip = nextBGM;
        m_BGMAudioSource.Play();
        var vol2OneTweener = DOTween.To(() => m_BGMAudioSource.volume, delegate (float x)
        {
            m_BGMAudioSource.volume = x;
        }, 0.6f, 1.5f);
        m_CurrentBGM = nextBGMName;
        await vol2OneTweener;
    }
}